#ifndef __CLIENT_REQUEST_REGISTER_included
#define __CLIENT_REQUEST_REGISTER_included

#include <boost/lexical_cast.hpp>
#include "../engine/Request.h"
#include "../http-auth/md5.h"
#include "../http-auth/http_author_util.h"
#include "../port/System.h"
#include "../client-common/Config.h"

using namespace Engine;
using namespace std;

class CRequestRegister : public CRequest {

public :

	CRequestRegister() : CRequest(string(COMMAND_REGISTER)) 
	{
	}

	CRequestRegister(string& code) : CRequest(string(COMMAND_REGISTER)) 
	{
		if(code.length() > 0) {
			AddParameter(string("authcode"), code);
		}
		else {
			Util::CSystem::TimeType now  = Util::CSystem::GetCurrentSeconds();
			string s = boost::lexical_cast<string>(now);
			MD5_CTX Md5Ctx;
			unsigned char hash[16];
			MD5_Init(&Md5Ctx);
			MD5_Update(&Md5Ctx, (unsigned char*)s.c_str(), s.length());
			MD5_Final(hash, &Md5Ctx);

			char* enc;
			LibBase64_Encode((unsigned char*)hash, sizeof(hash), (unsigned char**)&enc);
			for(char* p = enc; *p; p++) {
				if(*p == '\r') *p = 'r';
				else if(*p == '\n') *p = 'n';
			}
			
			Client::Common::CConfig& conf = Client::Common::CConfig::GetInstance();
			conf.mPassword = enc;
			this->AddParameter(string("password"), conf.mPassword);

			delete enc;
		}
	}

};

#endif // __CLIENT_REQUEST_REGISTER_included